package test.oldcase;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import test.pojo.Contact;
import test.pojo.MySQLShowProcessList;
import test.pojo.Users;
 

import com.javabloger.orm.SimpleDataSource;
import com.javabloger.orm.common.ColumnMapping;
import com.javabloger.orm.common.ORMappingTools;

		public class JAVABeanTestCase {
		
			/**
			 * @param args
			 * @throws SQLException 
			 */
			
			static String dirver="com.mysql.jdbc.Driver" ;
			static String url="jdbc:mysql://127.0.0.1/smbcrm";
			static String username="root";
			static String password="123456";
			
			public static void main(String[] args) throws Exception {
				 
				Class<?> clazz1= Users.class ;   /// ����
				Object paraObject=clazz1.newInstance();
				
				Field  Fields[] =paraObject.getClass().getDeclaredFields();
 
		       try{
		      
		        for (int i = 0; i < Fields.length ; i++) {
		            
//		        	Class<?> propType = Fields[i].getType();
		        	
		        	ColumnMapping columnMapping=Fields[i].getAnnotation(ColumnMapping.class) ;
		        	 Object value =null;
		        	 if (columnMapping!=null){
		        		 value =  getResultSetValues( columnMapping.columnName() );
		        		 System.out.print   (columnMapping.columnName()+" |  Method :");
		        		 String setMethodName = "set" + Fields[i].getName().substring(0,1).toUpperCase()+Fields[i].getName().substring(1) ;       
		        		 System.out.print   ( setMethodName+" | ");
		        		 System.out.println  (value);
		        		 Method setMethod = paraObject.getClass().getMethod(setMethodName, new Class[]{ Fields[i].getType() }); 
 		        		 setMethod.invoke(paraObject, new Object[]{value } );  
		        	 }
		        	 else{
		        		 value =getResultSetValues(Fields[i].getName());
		        		 String setMethodName = "set" + Fields[i].getName().substring(0,1).toUpperCase()+Fields[i].getName().substring(1) ;       
		         		 System.out.print  (Fields[i].getName() +" |  Method :");
		         		 System.out.println  (setMethodName+"  |  "+value);
		         		 Method setMethod = paraObject.getClass().getMethod(setMethodName, new Class[]{ Fields[i].getType() }); 
 		         		setMethod.invoke(paraObject, new Object[]{value } );  
		        	 }
		             	
		        
		        }  // for 
		      
		        Users user=(Users) paraObject;
		        
		          System.out.println (user.getCreatetime( ) );
		        }
		        catch (Exception e){
		     	   e.printStackTrace();
		        }
		              
			}
			
			
			public static Object getResultSetValues(String columnName){
				Object obj=null;
				try{
				 Connection  conn= new SimpleDataSource(
						 dirver, url,
						 username, password).getConnection();
				 ResultSet rs=conn.prepareStatement("select * from crm_users where id=2").executeQuery() ;
				if (rs.next()){
//					System.out.println (columnName);
					 obj=rs.getObject(columnName);
				}
				}
				catch (Exception e){
					e.printStackTrace();
				}
				 
				 return obj;
			}
			
		
}
